import { createSlice } from '@reduxjs/toolkit'
 
// 定义状态类型  
interface Action {  
  payload:number,
  type: string
} 
interface State {  
  count: number,
  defaultSelectedKeys:Array<any>,
  defaultOpenKeys:Array<any>
} 
 
export const counterStore = createSlice({
  // 模块名称独一无二
  name: 'counter',
  // 初始数据
  initialState: {
    count: 15,
    defaultSelectedKeys:[""],//初始选中的菜单项
    defaultOpenKeys:[""]//初始展开的 SubMenu 菜单项
    
  },
  // 修改数据的同步方法
  reducers: {
    setDefRouter:(state:State,defRouterDat:any)=>{
     
      state.defaultOpenKeys=defRouterDat.payload.split(',')
    },
    setDefSelectedKey:(state:State,defRouterDat:any)=>{
     
      state.defaultSelectedKeys=defRouterDat.payload.split(',')
    },
    // 清空路由
    clearRouter:(state:State)=>{
      state.defaultSelectedKeys=["home"]
      state.defaultOpenKeys=["home"]
    },
		increment: (state:State) => {
      state.count = 1
    },
    decrement: (state:State) => {
      state.count -= 1
    },
    incrementNum: (state:State, action: Action) => {
      state.count += action.payload
    }
  }
})
 
// 导出
export const { clearRouter,increment,setDefSelectedKey, decrement, incrementNum,setDefRouter } = counterStore.actions
export default counterStore.reducer